<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class UserVisitorModel extends Model
{

	/**
	 * 记录来访(半小时内重复来访不刷新记录)
	 *
	 * @param int $userID 被访问者ID
	 * @param int $visitorID 来访者ID
	 * @return null
	 */
	public function addVisitLog($userID, $visitorID)
	{
	    $map = array(
	        'user_id' => $userID,
	        'visitor_id' => $visitorID,
	    );
	    $result = $this->where($map)->find();
	    if (empty($result))
	    {
	    	$data = array(
		        'user_id' => $userID,
		        'visitor_id' => $visitorID,
		        'create_time' => time(),
		    );
	    	$this->add($data);
	    }
	    else 
	    {
	    	$this->where($map)->setField('create_time', time());	
	    }
	}
	
	
	/**
	 * 删除过期的来访记录
	 * @param int $day
	 * @return null
	 */
	public function rmVisitLogs($day=0)
	{
		$time = empty($day) ? 7 * 86400 : $day * 86400;
		$where = array('create_time' => array('lt', time() - $time));
		$this->where($where)->delete();
	}
	
	
	/**
	 * 获取最近访客
	 *
	 * @param int $userID
	 * @param int $limit
	 * @return array
	 */
	public function getRecentVisitors($userID, $limit=10)
	{
		$visitors = array();
		$map = array('user_id' => $userID);
		$visitors = $this->where($map)->limit($limit)->select();
		return $visitors;	
	}

}

/* End of file UserVisitorModel.class.php */
/* Location: ./App/Lib/Model/UserVisitorModel.class.php */